﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;

    [StandardModule]
    public sealed class modGeneral
    {
        public static string ConnectionString;
        public static enumDBAccessType DBAccessType;
        public static string H_ACCNORECEIPTNOTE;
        public static string H_ACCOUNTNAME;
        public static string H_ACCOUNTNUMBERUSD;
        public static string H_ACOUNTTNUMBERVND;
        public static string H_ADDRESS;
        public static string H_ADDRESSANH;
        public static string H_ADDRESSVIET;
        public static string H_APPLOCATION;
        public const string H_APPNAME = "ERP";
        public static string H_BANK;
        public static string H_BANKADDESS;
        public static bool H_CANHBAOHANGTONKHO = false;
        public static bool H_CHONHANGXUATTUDANHMUC = false;
        public static bool H_CHOPHEPAMQUY = false;
        public static bool H_CHOPHEPSUAGIA = false;
        public static bool H_CHOPHEPXUATAM;
        public static string H_CLIENT;
        public static string H_COMPANYNAMEANH;
        public static string H_COMPANYNAMEVIET;
        public static string H_COQUANTHUE = "";
        public static string H_CURRENCY;
        public static int H_DANHAPTTCT = 0;
        public static DataTable H_DATACOMPANY;
        public static string H_DBDATANAME = "";
        public static string H_DBPASS = "";
        public static string H_DBSERVERNAME = "";
        public static string H_DBUSER = "";
        public static string H_DCGIAOHANG;
        public static HTDefaultValue H_DEFAULT = new HTDefaultValue();
        public static string H_DONVICHUQUAN = "";
        public static clsDungChung H_DUNGCHUNG = new clsDungChung();
        public static string H_EMAIL;
        public static string H_EMPLOYEE;
        public static string H_FAX;
        public static string H_FIRSTDAY = "";
        public static string H_FORMATNUMBER;
        public static bool H_GHISODONGTHOI;
        public static bool H_GIABANBAOGOMTHUE;
        public static string H_GIACHUAN;
        public static bool H_GIOIHANDULIEU = false;
        public const string H_HELPFILE = "Dragon_Help.chm";
        public static string H_HOTLINE;
        public static int H_ISCLIENT = 1;
        public static bool H_KETHOPDULIEUTHUE = true;
        public static string H_KEYAPPNAME;
        public static bool H_KHONGXUATQUACONGNO;
        public static string H_LANGUAGE = "VIET";
        public static string H_LOAIGIA;
        public static string H_LOAIHOADONINMACDINH = "";
        public static string H_LOAIINMACDINH = "";
        public static string H_LOCKDATE;
        public static string H_MAUHOADONMACDINH = "";
        public static string H_MSTDONVICHUQUAN = "";
        public static string H_PHIENBANQUYETDINH = "";
        public static string H_PHUONGPHAPTINHGIAVON;
        public static DataTable H_PPPB;
        public static string H_PREFIX;
        public static int H_SOLIENIN = 1;
        public static string H_STOCKID;
        public static string H_SWIFTCODE;
        public static string H_TAXCODE;
        public static string H_TELEPHONE;
        public static string H_THAYDOICHUNGTU;
        public static bool H_THEODOIHANDUNG;
        public static bool H_THEODOIQUAYKE;
        public static bool H_THEODOISERI;
        public static bool H_THEODOISERIALKHIXUAT;
        public static bool H_THEODOISOLOHANG;
        public static string H_USERGROUP;
        public static string H_USERID;
        public static string H_USERNAME;
        public static string H_USERTYPE;
        public static string H_VALUE;
        public static string H_WEBSITE;
        [DateTimeConstant(0x2bca27acc9cd8000L)]
        public static readonly DateTime MaxDate = new DateTime(0x2bca27acc9cd8000L);
        public const string OIDNull = "";
        [DateTimeConstant(0x7e0e8a3f8ca4000L)]
        public static readonly DateTime ZeroDate = new DateTime(0x7e0e8a3f8ca4000L);

        public static object adjustNullDB(object DBValue)
        {
            return Interaction.IIf(Information.IsDBNull(RuntimeHelpers.GetObjectValue(DBValue)), string.Empty, RuntimeHelpers.GetObjectValue(DBValue));
        }

        public static string adjustNullString(string stringValue)
        {
            return Conversions.ToString(Interaction.IIf(stringValue == null, string.Empty, stringValue));
        }

        private static string ByteArrayToString(byte[] arrInput)
        {
            StringBuilder builder = new StringBuilder(arrInput.Length);
            int num2 = arrInput.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                builder.Append(arrInput[i].ToString("X2"));
            }
            return builder.ToString();
        }

        public static string CapSoHoaDon(string strMauHoaDon, string strSoKiHieu, int intPreview)
        {
            IDataParameter[] commandParameters = new IDataParameter[] { DBHelper.createParameter("@MauHoaDon", DbType.Int16, ParameterDirection.Input, strMauHoaDon), DBHelper.createParameter("@SoKiHieu", DbType.Int16, ParameterDirection.Input, strSoKiHieu), DBHelper.createParameter("@Preview", DbType.Double, ParameterDirection.Input, intPreview), DBHelper.createParameter("@GiaTri", DbType.Double, ParameterDirection.Output, 0), DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0) };
            int num = DBHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Sp_CapSoHoaDon", commandParameters);
            if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[4].Value, 0, false))
            {
                LVYModule.ShowWarning(Tranlated("K\x00ed hiệu n\x00e0y đ\x00e3 hết h\x00f3a đơn !", ""));
            }
            return Strings.Right(Conversions.ToString(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject("0000000", commandParameters[3].Value)), 7);
        }

        public static string CreatKey(string pTableName, int pLenght, string pType = "")
        {
            pLenght = 15;
            IDataParameter[] commandParameters = new IDataParameter[] { DBHelper.createParameter("@SoKhoa", DbType.Int64, ParameterDirection.Output, 0), DBHelper.createParameter("@TenBang", DbType.Int64, ParameterDirection.Input, pTableName), DBHelper.createParameter("@ChieuDai", DbType.Int64, ParameterDirection.Input, 9) };
            DBHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Sp_CapKhoa", commandParameters);
            string str3 = commandParameters[0].Value.ToString();
            string str2 = H_PREFIX + pType.Trim();
            return (str2 + Strings.Right("000000000000000" + str3.Trim(), pLenght - str2.Length));
        }

        public static string Decrypt(string pstrText)
        {
            if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(LVYModule.ReturnIfNull(pstrText, "", TypeOfValue.Nothing), "", false))
            {
                return "";
            }
            pstrText = pstrText.Replace(" ", "+");
            string str2 = "1239;[pewGKG)NisarFidesTech";
            byte[] rgbKey = new byte[0];
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] buffer = new byte[(pstrText.Length - 1) + 1];
            rgbKey = Encoding.UTF8.GetBytes(str2.Substring(0, 8));
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            buffer = Convert.FromBase64String(pstrText);
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream = new CryptoStream(stream2, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            stream.Write(buffer, 0, buffer.Length);
            stream.FlushFinalBlock();
            return Encoding.UTF8.GetString(stream2.ToArray());
        }

        public static string Encrypt(string pstrText)
        {
            string str2 = "1239;[pewGKG)NisarFidesTech";
            byte[] rgbKey = new byte[0];
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            rgbKey = Encoding.UTF8.GetBytes(str2.Substring(0, 8));
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.UTF8.GetBytes(pstrText);
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream = new CryptoStream(stream2, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            stream.Write(bytes, 0, bytes.Length);
            stream.FlushFinalBlock();
            return Convert.ToBase64String(stream2.ToArray());
        }

        public static bool Exists(string OID, string tableName)
        {
            string commandText = "SELECT OID from [" + tableName + "] where OID = '" + OID + "'";
            OID = Conversions.ToString(DBHelper.ExecuteScalar(ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
            return ((OID != null) && !OID.Equals(string.Empty));
        }

        public static int getNewSequence(string TableName)
        {
            IDataReader reader=null;
            int num2 = 1;
            try
            {
                string commandText = "SELECT MAX([Sequence]) FROM " + TableName;
                reader = DBHelper.ExecuteReader(ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (reader.Read())
                {
                    num2 = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.AddObject(GetValue(reader, 0, typeOfColumn.GetInt32), 1));
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw exception;
                ProjectData.ClearProjectError();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return num2;
        }

        public static object GetValue(IDataReader dataReader, int column, typeOfColumn ColumnType)
        {
            object @float=0.0;
            IDataReader reader = dataReader;
            if (reader.IsDBNull(column))
            {
                switch (((int) ColumnType))
                {
                    case 1:
                        @float = "";
                        break;

                    case 2:
                        @float = 0;
                        break;

                    case 3:
                        @float = 0;
                        break;

                    case 4:
                        @float = 0;
                        break;

                    case 5:
                        @float = new DateTime(0x7e0e8a3f8ca4000L);
                        break;

                    case 8:
                        @float = 0;
                        break;

                    case 9:
                        @float = 0;
                        break;
                }
            }
            else
            {
                switch (((int) ColumnType))
                {
                    case 1:
                        @float = reader.GetString(column);
                        break;

                    case 2:
                        @float = reader.GetInt32(column);
                        break;

                    case 3:
                        @float = reader.GetInt64(column);
                        break;

                    case 4:
                        @float = reader.GetFloat(column);
                        break;

                    case 5:
                        @float = reader.GetDateTime(column);
                        break;

                    case 8:
                        @float = reader.GetBoolean(column);
                        break;

                    case 9:
                        @float = reader.GetDecimal(column);
                        break;
                }
            }
            reader = null;
            return @float;
        }

        public static string MD5Encrypt(string flatString)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            return ByteArrayToString(provider.ComputeHash(encoding.GetBytes(flatString)));
        }

        public static string subStringSafe(string stringVal, int startIndex, int length = 0)
        {
            string str = string.Empty;
            try
            {
                if (length == 0)
                {
                    str = stringVal.Substring(startIndex);
                }
                else
                {
                    str = stringVal.Substring(startIndex, length);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                string str2 = str;
                ProjectData.ClearProjectError();
                return str2;
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public static string Tranlated(string pString, string Language = "")
        {
            string str2;
            if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(NewLateBinding.LateGet(LVYModule.ReturnIfNull(pString, "", TypeOfValue.Nothing), null, "Trim", new object[0], null, null, null), "", false))
            {
                return "";
            }
            if (Language == "")
            {
                Language = H_LANGUAGE;
            }
            if (Language.Trim().ToUpper() == "VIET")
            {
                str2 = pString;
            }
            else
            {
                string commandText = "SELECT " + Language + " FROM HT_Dictionary  WHERE RTRIM(VIET) = N'" + pString.Trim() + "'";
                str2 = Conversions.ToString(DBHelper.ExecuteScalar(ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
                if (LVYModule.ReturnIfNull(str2, "", TypeOfValue.Nothing).ToString().Trim() == "")
                {
                    commandText = "INSERT INTO HT_Dictionary (VIET,ANH) VALUES (N'" + pString + "', N'" + pString + "')";
                    DBHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                    str2 = pString;
                }
            }
            return str2;
        }

        public enum enumAccountStatus
        {
            Active,
            Deactive
        }

        public enum enumAuthenticateResult
        {
            ExpireAccount = 2,
            InvalidAccount = 1,
            Passed = 3
        }

        public enum enumDBAccessType
        {
            oledb = 1,
            sql = 2
        }

        public enum enumOrderType
        {
            Delivery = 1,
            EatIn = 3,
            Pickup = 2
        }

        public enum ListDanhMuc
        {
            DTTHCP =0x31,
            LoaiDTTHCP = 0x30,
            Blank = 0x29,
            BoPhan = 4,
            ChiTietDonViTinh = 8,
            DoiTuongNganHang = 11,
            DoiTuongNhanVien = 0x22,
            DonVi = 5,
            DonViTinh = 0x11,
            HangHoa = 2,
            HangMucBaoCaoThue = 0x24,
            HangSanXuat = 3,
            KhoanMucChiPhi = 0x1c,
            KhoaTinhTrang = 0x15,
            KhoHang = 0x18,
            KhuVuc = 10,
            LoaiChiPhi = 0x12,
            LoaiGiaBan = 0x10,
            LoaiHoaDon = 13,
            LoaiHoaDonIn = 0x26,
            LoaiIn = 0x25,
            LoaiKhachHang = 12,
            LoaiNhapXuat = 0x1d,
            LoaiTaiSan = 0x13,
            LoaiTien = 14,
            LyDoTangTaiSan = 0x1a,
            MauHoaDon = 0x27,
            NganhHang = 0x21,
            NganhKinhDoanh = 6,
            NhomHangHoa = 1,
            NhomNguoiDung = 0x1f,
            NhomThucDon = 15,
            PhuongThucTamUng = 0x23,
            PhuongThucThanhToan = 7,
            PhuongThucVanChuyen = 30,
            QuocGia = 20,
            SoKiHieu = 40,
            TenDangNhap = 0x19,
            TenPhuTung = 0x1b,
            ThongSoKyThuat = 0x20,
            TinhThanh = 9,
            TyGia = 0x16,
            ViTri = 0x17
        }

        public enum Result
        {
            Error = 0,
            Successful = -1
        }

        public enum TypeObjects
        {
            TatCa,
            NhaCungCap,
            KhachHang,
            NhanVien,
            NganHang
        }

        public enum typeOfColumn
        {
            GetBinary = 7,
            GetBit = 8,
            GetDateTime = 5,
            GetDecimal = 9,
            GetDouble = 6,
            GetFloat = 4,
            GetInt32 = 2,
            GetInt64 = 3,
            GetString = 1
        }

        public enum TypeOfValue
        {
            Nothing,
            Number,
            String
        }
    }
}

